class Reservation < ActiveRecord::Base
  
  validate :validate_guest_cpf, :validate_dates, :validate_avaialable_room
  validates_presence_of(:guest_cpf)
  validates_numericality_of(:num_adults, :num_childre)
  
    
   def validate_guest_cpf
     guest = Guest.find_by_cpf(guest_cpf)
     if guest.nil?
       errors.add(:guest_cpf,"not registered")
     end
   end
 
 def validate_dates
   date = Date.new
   if(arrive_date.to_s > exit_date.to_s)
    errors.add("Date choice")
  else
    if(arrive_date.to_s < date.to_s)
      errors.add("Date choice")
    end  
   end
 end

def validate_avaialable_room
  num_persons = num_childre + num_adults
  room = Room.find_empty_room(num_persons)
  if room.nil?
    errors.add("no room avaialable")
  else
    self.room_id = room.num
    room.reserved = true
    room.update_attribute(:reserved, true)
  end   
end

  
end
